package activities;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.evaluity.R;

import logic.Answer;
import logic.Question;
import logic.Test;

import data.DATA_Helper;
import data.DB_Helper;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.ListActivity;
import android.content.Intent;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class TestsList extends ListActivity {
	public static final String EXTRA_COURSE_ID = "course_id";
	public static final String EXTRA_RECENT_TESTS = "recent_tests";
	private HashMap<Integer, Test> testsMap = new HashMap<Integer, Test>();
	private boolean offlineMenu = false;

	private void insertFakeTests() {
		DB_Helper db = new DB_Helper(this, null);
		List<Answer> answers1 = new ArrayList<Answer>();
		answers1.add(new Answer("1"));
		answers1.add(new Answer("2"));
		answers1.add(new Answer("3"));
		Question q1 = new Question("1+1?", answers1);

		List<Answer> answers2 = new ArrayList<Answer>();
		answers2.add(new Answer("4"));
		answers2.add(new Answer("5"));
		answers2.add(new Answer("6"));
		Question q2 = new Question("2+2?", answers2);

		List<Answer> answers3 = new ArrayList<Answer>();
		answers3.add(new Answer("7"));
		answers3.add(new Answer("8"));
		answers3.add(new Answer("9"));
		Question q3 = new Question("4-1?", answers3);

		List<Question> questions = new ArrayList<Question>();
		questions.add(q1);
		questions.add(q2);
		questions.add(q3);

		db.insertTest(new Test("Test1", questions));
		db.insertTest(new Test("Test2", questions));
		db.insertTest(new Test("Test3", questions));
	}

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_tests_list);

		Intent i = getIntent();
		offlineMenu = i.getBooleanExtra("offline", false);
		List<Test> tests;

		if (offlineMenu)
			tests = DATA_Helper.getOfflineTests();

		else {
			String courseId = i.getStringExtra(EXTRA_COURSE_ID);
			if (courseId != null) 
				tests = DATA_Helper.getTestsByCourse(courseId);
			else {
				tests = (List<Test>) i.getSerializableExtra(EXTRA_RECENT_TESTS);
//				tests = DATA_Helper.getTestSet(testsIds);
			}
		}

		List<String> testsNames = new ArrayList<String>();
		
		if (tests.size() > 0) {
			Test test;
			for (int i1 = 0; i1 < tests.size(); i1++) {
				test = tests.get(i1);
				if (!offlineMenu && DATA_Helper.existsTest(test.getId()))
					test.switchOffline();
				testsNames.add(test.getName());
				testsMap.put(i1, test);
			}

			ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
					android.R.layout.simple_list_item_1, testsNames);
			registerForContextMenu(getListView());
			
			setListAdapter(adapter);
		}

		else {
			String message = "Ops! it looks like this list doesn't contain any test yet";
			TextView tv = (TextView) findViewById(R.id.nomLabel);
			tv.setText("");
			testsNames.add(message);
			ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
					android.R.layout.simple_list_item_1, testsNames);
			registerForContextMenu(getListView());
			setListAdapter(adapter);
			getListView().setEnabled(false);
		}
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		MenuInflater inflater = getMenuInflater();

		AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
		int pos = info.position;
		if (testsMap.get(pos).isOffline()) {
			menu.add(0, v.getId(), 0, "Disable offline");
		} else {
			menu.add(0, v.getId(), 0, "Enable offline");
		}
		// info.position is the position in the ListView

		inflater.inflate(R.menu.context_menu, menu);

		menu.setHeaderTitle("Switch online/offline");

	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
				.getMenuInfo();
		Test selectedTest = testsMap.get(info.position);

		if (selectedTest.isOffline()) {
			selectedTest.switchOffline();
			if (DATA_Helper.deleteTestFromDB(selectedTest)) {
				Toast.makeText(this, "Switch online successful",
						Toast.LENGTH_SHORT).show();
			}
		} else {
			selectedTest.switchOffline();
			List<Question> questions = DATA_Helper
					.getQuestionsByTest(selectedTest.getId());
			for (Question q : questions) {
				q.setAnswers(DATA_Helper.getAnswersByQuestion(q.getId()));
			}
			selectedTest.setQuesions(questions);
			DATA_Helper.insertTest(selectedTest);
			Toast.makeText(this, "Switch offline successful", Toast.LENGTH_SHORT)
					.show();
		}
		return true;
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		Test selectedTest = testsMap.get(position);
		
		DATA_Helper.insertTestAccess(selectedTest.getId(), selectedTest.getName());
		
		Intent i = new Intent(this, QuestionsList.class);
		i.putExtra(QuestionsList.EXTRA_TEST_ID, selectedTest.getId());
		i.putExtra("offline", offlineMenu);

		if (offlineMenu) {
			i.putExtra("test", selectedTest);
		}

		startActivity(i);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.tests_list, menu);
		return true;
	}
}
